package xcmg.device.web.basic;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.basic.MaterialsSupplierService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.basic.MaterialsSupplierVO;
import xcmg.device.service.vo.company.CompanySupplierVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/materialsSupplier")
public class MaterialsSupplierController extends BasicController {

  @Autowired
  private MaterialsSupplierService materialsSupplierService;

  @RequestMapping(value = "/remove", method = RequestMethod.GET)
  @FastMappingInfo(needLogin = true)
  public ActionResult remove(String id) {
    return actionValueResult(materialsSupplierService.remove(id));
  }

  @RequestMapping(value = "/list", method = RequestMethod.POST)
  @FastMappingInfo(needLogin = true)
  public ActionResult listByMaterialsId(@RequestBody SearchCommonVO<MaterialsSupplierVO> searchVO) throws Exception {
    PageCommonVO<MaterialsSupplierVO> materialsSuppliers = materialsSupplierService.list(searchVO,getOrgId());
    return actionValueResult(materialsSuppliers.getPageInfo());
  }

  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @FastMappingInfo(needLogin = true)
  public ActionResult add(@RequestBody MaterialsSupplierVO materialsSupplierVO) {

    ActionResult result = new ActionResult();
    int count = 0;
    try {
      materialsSupplierVO.setOrgId(getOrgId());
      materialsSupplierVO.setCreateId(getUserId());
      materialsSupplierVO.setCreateName(getUserName());
      count = materialsSupplierService.add(materialsSupplierVO);
    } catch (Exception e) {
      result.setCode(ErrorCode.IllegalArument.getCode());
      result.setMessage(e.getMessage());
      return result;
    }
    result.setCode(ErrorCode.Success.getCode());
    result.setValue(count);
    return result;
  }

  @ResponseBody
  @RequestMapping(value = "/import", method = RequestMethod.POST)
  @ApiOperation("导入供应商")
  @FastMappingInfo(needLogin = true)
  public ActionResult importExcel(HttpServletRequest request) throws Exception {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
    String fileName = file.getOriginalFilename();
    FunctionResult<String> result=new FunctionResult<>();
    if (!fileName.trim().endsWith(".xls")) {
      result.setCode(ErrorCode.ImportExcelEndWithXLS);
    } else {
      result= materialsSupplierService.importFormExcel(file, getOrgId(), getUserId());
    }
    return actionResult(result.getCode(), result.getValue());
  }


  @RequestMapping(value = "/download", method = RequestMethod.POST)
  @ApiOperation("下载导入零件供应商的模板")
  @FastMappingInfo(needLogin = true)
  public void download() throws Exception {
    materialsSupplierService.download();
  }

  @RequestMapping(value = "/listSuppliers", method = RequestMethod.POST)
  @ApiOperation("获取可用的直投供应商")
  @FastMappingInfo(needLogin = true)
  public ActionResult listSuppliers(@RequestBody SearchCommonVO<MaterialsSupplierVO> searchVO) {
    PageCommonVO<CompanySupplierVO> suppliersPage =  materialsSupplierService.listSuppliers(searchVO);
    return actionValueResult(suppliersPage.getPageInfo());
  }
}
